package com.example.myapplication.government;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BenefitBean;
import com.example.myapplication.ShuJu.ClassificationlistBean;
import com.example.myapplication.ShuJu.DemandsBean;
import com.example.myapplication.apter.DemandApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DemandsActivity extends AppCompatActivity {

    private RecyclerView re_demand;
    private ClassificationlistBean classificationlistBean;
    private Gson gson;
    private String DemandsActivityID;
    private Request request;
    private OkHttpClient okHttpClient;
    private DemandsBean demandsBean;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demands);
        Intent intent = getIntent();
        DemandsActivityID = intent.getStringExtra("DemandsActivityID");
        initview();
       senddemands();
    }

    private void senddemands() {
        request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/appeal/list?appealCategoryId="+DemandsActivityID+"&pageNum=1&pageSize=10")
                .addHeader("Authorization", TokenBean.token)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("xx","bb");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =  response.body().string();
                Log.d("xjson",json);
                gson = new Gson();
                demandsBean = gson.fromJson(json, DemandsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        bindemands();
                    }
                });
            }
        });
    }

    private void bindemands() {
        DemandApter demandApter = new DemandApter(DemandsActivity.this,demandsBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DemandsActivity.this);
        re_demand.setLayoutManager(linearLayoutManager);
        re_demand.setAdapter(demandApter);
    }

    private void initview() {
        gson = new Gson();
        re_demand = findViewById(R.id.re_demand);
        handler = new Handler();
        okHttpClient = new OkHttpClient();
    }
}